package ML

import (
	"AttackTracer/Analyzer/config"
	"bytes"
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
)

var MLConfig config.MachineLearning

func ConnectMLServer(mlConfig config.MachineLearning, packetChan chan PacketInfo) {
	MLConfig = mlConfig
	client := &http.Client{}
	r := Response{}
	resp, err := client.Get("http://" + MLConfig.ServerAddr)
	if err == nil {
		body, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			//log.Println(string(body))
			_ = json.Unmarshal(body, &r)
		}
	}
	log.Println("Response:", r.Message)

	for tmpPacket := range packetChan {
		log.Println("Requesting ML server")
		go func() {
			r := Response{}
			postBody, _ := json.Marshal(tmpPacket)
			resp, err := client.Post("http://"+MLConfig.ServerAddr+"/ML/",
				"application/json", bytes.NewReader(postBody))
			if err == nil {
				body, err := ioutil.ReadAll(resp.Body)
				if err == nil {
					//log.Println(string(body))
					_ = json.Unmarshal(body, &r)
				}
			}
			log.Println("ML Response:", r.Message)
		}()
	}
}

type Response struct {
	Message string `json:"message"`
}

type PacketInfo struct {
	Duration               []int   `json:"duration"`
	ProtocolType           string  `json:"protocol_type"`
	Service                string  `json:"service"`
	Flag                   string  `json:"flag"`
	SrcBytes               int     `json:"src_bytes"`
	DstBytes               int     `json:"dst_bytes"`
	Land                   int     `json:"land"`
	WrongFragment          int     `json:"wrong_fragment"`
	Urgent                 int     `json:"urgent"`
	Ho                     int     `json:"ho"`
	NumFailedLogins        int     `json:"num_failed_logins"`
	LoggedIn               int     `json:"logged_in"`
	NumCompromised         int     `json:"num_compromised"`
	RootShell              int     `json:"root_shell"`
	SuAttempted            int     `json:"su_attempted"`
	NumRoot                int     `json:"num_root"`
	NumFileCreations       int     `json:"num_file_creations"`
	NumShells              int     `json:"num_shells"`
	NumAccessFiles         int     `json:"num_access_files"`
	NumOutboundCmds        int     `json:"num_outbound_cmds"`
	IsHostLogin            int     `json:"is_host_login"`
	IsGuestLogin           int     `json:"is_guest_login"`
	Count                  int     `json:"count"`
	SrvCount               int     `json:"srv_count"`
	SerrorRate             float32 `json:"serror_rate"`
	SrvSerrorRate          float32 `json:"srv_serror_rate"`
	RerrorRate             float32 `json:"rerror_rate"`
	SrvRerrorRate          float32 `json:"srv_rerror_rate"`
	SameSrvRate            float32 `json:"same_srv_rate"`
	DiffSrvRate            float32 `json:"diff_srv_rate"`
	SrvDiffHostRate        float32 `json:"srv_diff_host_rate"`
	DstHostCount           int     `json:"dst_host_count"`
	DstHostSrvCount        int     `json:"dst_host_srv_count"`
	DstHostSameSrvRate     float32 `json:"dst_host_same_srv_rate"`
	DstHostDiffSrvRate     float32 `json:"dst_host_diff_srv_rate"`
	DstHostSameSrcPortRate float32 `json:"dst_host_same_src_port_rate"`
	DstHostSrvDiffHostRate float32 `json:"dst_host_srv_diff_host_rate"`
	DstHostSerrorRate      float32 `json:"dst_host_serror_rate"`
	DstHostSrvSerrorRate   float32 `json:"dst_host_srv_serror_rate"`
	DstHostRerrorRate      float32 `json:"dst_host_rerror_rate"`
	DstHostSrvRerrorRate   float32 `json:"dst_host_srv_rerror_rate"`
	Class                  string  `json:"class"`
}
